package com.lkx.farmershop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkx.farmershop.common.api.ApiResult;
import com.lkx.farmershop.mapper.JoinReasonInfoMapper;
import com.lkx.farmershop.model.entity.JoinReasonInfo;
import com.lkx.farmershop.model.entity.Tuan;
import com.lkx.farmershop.model.entity.TuanMember;
import com.lkx.farmershop.service.JoinReasonInfoService;
import com.lkx.farmershop.service.TuanMemberService;
import com.lkx.farmershop.service.TuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (JoinReasonInfo)表服务实现类
 *
 * @author makejava
 * @since 2025-02-01 22:10:51
 */
@Service("joinReasonInfoService")
public class JoinReasonInfoServiceImpl extends ServiceImpl<JoinReasonInfoMapper, JoinReasonInfo> implements JoinReasonInfoService {

    @Autowired
    private TuanMemberService tuanMemberService;
    @Autowired
    private TuanService tuanService;

    @Override
    public ApiResult<String> refuseJoin(Long id) {
        //删除该加入申请
        removeById(id);
        return ApiResult.success("已拒绝该加入申请");
    }

    @Override
    public ApiResult<String> agreeJoin(Long id) {
        //将该申请用户加入组团成员表
        JoinReasonInfo joinReasonInfo = getById(id);
        Long tuanId = joinReasonInfo.getTuanId();
        Long userId = joinReasonInfo.getUserId();
        tuanMemberService.save(new TuanMember(tuanId, userId, false));

        //将该组团的已加入人数+1
        Tuan tuan = tuanService.getById(tuanId);
        tuan.setJoinPeople(tuan.getJoinPeople()+1);
        tuanService.updateById(tuan);

        //删除该加入申请
        removeById(id);
        return ApiResult.success("已同意该加入申请");
    }
}

